
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class GuideFragment extends Fragment {

    public static final String FLAG_COME = "come_flag";

    public static final int COME_FIRST = 0;

    public static final int COME_ABOUT = 1;

    private int mFlag;

    private ViewPager mPager;

    private List<ImageView> mGuideListView;

    private int[] mGuideIMGid = {
            R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3, R.drawable.guide_4
    };

    public GuideFragment() {
        mGuideListView = new ArrayList<ImageView>();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFlag = getArguments() != null ? getArguments().getInt(FLAG_COME) : 0;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        clear();
    }

    private void clear() {
        if (mGuideListView == null || mGuideListView.isEmpty())
            return;
        int size = mGuideListView.size();
        for (int i = 0; i < size; i++) {
            ImageView imageview = mGuideListView.get(i);
            imageview = null;
        }
        mGuideListView.clear();
        mGuideListView = null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.cbh_guide, null);
        mPager = (ViewPager) view.findViewById(R.id.pager);

        final int len = mGuideIMGid.length;
        for (int i = 0; i < len; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(mGuideIMGid[i]);
            imageView.setScaleType(ScaleType.FIT_XY);
            mGuideListView.add(imageView);
        }
        mPager.setAdapter(new MyPagerAdapter());
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            int pos = 0;

            @Override
            public void onPageSelected(int position) {
                // Log.e("GuideFragment", "onPageSelected position ="+position);
            }

            @Override
            public void onPageScrolled(int postion, float positionOffset, int positionOffsetPixels) {
                pos = postion;
                // Log.e("GuideFragment",
                // "onPageScrolled postion="+postion+", positionOffset"+positionOffset+", positionOffsetPixels="+positionOffsetPixels);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // Log.e("GuideFragment",
                // "onPageScrollStateChanged state ="+state);
                if (pos == len - 1 && state == 0) {
                    next();
                }

            }
        });

        return view;
    }

    private void next() {
        if (mFlag == COME_ABOUT) {
            getActivity().finish();
            return;
        } else if (mFlag == COME_FIRST) {
            CBHActivity.come(getActivity());
            getActivity().finish();
        }
    }

    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return mGuideListView.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mGuideListView.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mGuideListView.get(position));
            return mGuideListView.get(position);
        }
    }

    public static GuideFragment newInstance(int come) {
        final GuideFragment f = new GuideFragment();

        final Bundle args = new Bundle();
        args.putInt(FLAG_COME, come);
        f.setArguments(args);

        return f;
    }

}
